package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.TableNameConstant;
import cc.rengu.igas.bomp.common.dao.BizTmpMapper;
import cc.rengu.igas.bomp.common.dao.MchntImageInfoMapper;
import cc.rengu.igas.bomp.common.dao.MchntSignInfoMapper;
import cc.rengu.igas.bomp.common.dao.RegionInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.BizTmpMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntImageInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntSignInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.RegionInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.BizTmp;
import cc.rengu.igas.bomp.common.entity.MchntImageInfo;
import cc.rengu.igas.bomp.common.entity.MchntSignInfo;
import cc.rengu.igas.bomp.common.entity.RegionInfo;
import cc.rengu.igas.bomp.common.enums.ImageTypeEnum;
import cc.rengu.igas.bomp.common.enums.QueryBizTypeEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.BusinessLicenceInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntArtifInfoBean;
import cc.rengu.igas.bomp.facade.bean.SignImageInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntSignInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntSignInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 商户签约信息查询
 * Created by 王成 on 2020/3/10
 */
public class MchntSignInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntSignInfoQueryRequest mchntSignInfoQueryRequest = new MchntSignInfoQueryRequest();
        ConvertUtil.convertOutput(mchntSignInfoQueryRequest);
        return mchntSignInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntSignInfoQueryRequest mchntSignInfoQueryRequest = (MchntSignInfoQueryRequest) request;
        MchntSignInfoQueryResponse mchntSignInfoQueryResponse = new MchntSignInfoQueryResponse();
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(mchntSignInfoQueryResponse);
        BeanUtil.beanCopy(mchntSignInfoQueryRequest, mchntSignInfoQueryResponse);
        MchntSignInfo mchntSignInfo = null;
        String bizType = mchntSignInfoQueryRequest.getHeader().getBizType();

        if (StringUtil.isEmptyOrNull(bizType)) {
            rglog.error("bizType<{}>为NULL!,无法查询信息", bizType);
            bizResponse.setRespCode(RespCodeEnum.PARAM_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.PARAM_ERROR.getRespDesc());
            bizResponse.setResult(mchntSignInfoQueryRequest);
            return bizResponse;
        }

        if (QueryBizTypeEnum.QUERY_TEMPORARY_TABLE.getType().equals(bizType)) {
            /* 查询临时表数据 */
            BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
            List<BizTmp> bizTmpList = bizTmpMapper.selectBizTmpByMchntNo(mchntSignInfoQueryRequest.getHeader().getInstId(), mchntSignInfoQueryRequest.getMchntNo(), TableNameConstant.T_B_MCHNT_SIGN_INFO);
            if (null != bizTmpList && !bizTmpList.isEmpty()) {
                BizTmp bizTmp = bizTmpList.get(0);
                String mchntSignInfoString = InitParamUtil.getJsonString(bizTmp);
                mchntSignInfo = JSON.parseObject(mchntSignInfoString, MchntSignInfo.class);
            }

            if (null != mchntSignInfo) {
                BeanUtil.beanCopy(mchntSignInfo, mchntSignInfoQueryResponse);

                BusinessLicenceInfoBean businessLicenceInfoBean = new BusinessLicenceInfoBean();
                BeanUtil.beanCopy(mchntSignInfo, businessLicenceInfoBean);

                MchntArtifInfoBean mchntArtifInfoBean = new MchntArtifInfoBean();
                BeanUtil.beanCopy(mchntSignInfo, mchntArtifInfoBean);
                /* 获取行政区域位置信息 */
                RegionInfoMapper regionInfoMapper = new RegionInfoMapperImpl();
                RegionInfo regionInfo = regionInfoMapper.selectRegionInfoByPrimaryKey(mchntSignInfo.getRegisterProvince());
                if (null != regionInfo) {
                    businessLicenceInfoBean.setRegisterProvinceName(regionInfo.getRegionName());
                }
                regionInfo = regionInfoMapper.selectRegionInfoByPrimaryKey(mchntSignInfo.getRegisterCity());
                if (null != regionInfo) {
                    businessLicenceInfoBean.setRegisterCityName(regionInfo.getRegionName());
                }
                regionInfo = regionInfoMapper.selectRegionInfoByPrimaryKey(mchntSignInfo.getRegisterArea());
                if (null != regionInfo) {
                    businessLicenceInfoBean.setRegisterAreaName(regionInfo.getRegionName());
                }

                List<BizTmp> imageList = bizTmpMapper.selectBizTmpByMchntNo(mchntSignInfoQueryRequest.getHeader().getInstId(), mchntSignInfoQueryRequest.getMchntNo(), TableNameConstant.T_B_MCHNT_IMAGE_INFO);
                if (!CollectionUtils.isEmpty(imageList)) {
                    for (BizTmp bizTmp : imageList) {
                        String mchntImageJson = InitParamUtil.getJsonString(bizTmp);
                        MchntImageInfo mchntImageInfo = JSON.parseObject(mchntImageJson, MchntImageInfo.class);
                        if (ImageTypeEnum.BUSINESS_LICENSE.getImageType().equals(mchntImageInfo.getImageType())) {
                            SignImageInfoBean businessImage = new SignImageInfoBean();
                            businessImage.setId(mchntImageInfo.getId());
                            businessImage.setImageIndex(Integer.toString(mchntImageInfo.getImageIndex()));
                            businessImage.setImageId(mchntImageInfo.getImageId());
                            businessImage.setAlipayImageId(mchntImageInfo.getAlipayImageId());
                            businessImage.setWechatImageId(mchntImageInfo.getWechatMediaId());
                            if (!StringUtil.isEmptyOrNull(mchntImageInfo.getImageRemotePath())) {
                                businessImage.setImagePath(Base64.encodeBase64String(mchntImageInfo.getImageRemotePath().getBytes()));
                            }
                            businessImage.setImageLocalPath(Base64.encodeBase64String(mchntImageInfo.getImageLocalPath().getBytes()));
                            businessImage.setImageType(mchntImageInfo.getImageType());
                            businessImage.setImageDesc(mchntImageInfo.getImageDesc());
                            businessLicenceInfoBean.setSignImageInfoBean(businessImage);
                        }

                        if (ImageTypeEnum.ARTIF_FACE_CERT_IMAGE.getImageType().equals(mchntImageInfo.getImageType())) {
                            SignImageInfoBean certFaceImage = new SignImageInfoBean();
                            certFaceImage.setId(mchntImageInfo.getId());
                            certFaceImage.setImageIndex(Integer.toString(mchntImageInfo.getImageIndex()));
                            certFaceImage.setImageId(mchntImageInfo.getImageId());
                            certFaceImage.setAlipayImageId(mchntImageInfo.getAlipayImageId());
                            certFaceImage.setWechatImageId(mchntImageInfo.getWechatMediaId());
                            if (!StringUtil.isEmptyOrNull(mchntImageInfo.getImageRemotePath())) {
                                certFaceImage.setImagePath(Base64.encodeBase64String(mchntImageInfo.getImageRemotePath().getBytes()));
                            }
                            certFaceImage.setImageLocalPath(Base64.encodeBase64String(mchntImageInfo.getImageLocalPath().getBytes()));
                            certFaceImage.setImageType(mchntImageInfo.getImageType());
                            certFaceImage.setImageDesc(mchntImageInfo.getImageDesc());
                            mchntArtifInfoBean.setCertFaceImage(certFaceImage);
                        }

                        if (ImageTypeEnum.ARTIF_BACK_CERT_IMAGE.getImageType().equals(mchntImageInfo.getImageType())) {
                            SignImageInfoBean certBackImage = new SignImageInfoBean();
                            certBackImage.setId(mchntImageInfo.getId());
                            certBackImage.setImageIndex(Integer.toString(mchntImageInfo.getImageIndex()));
                            certBackImage.setImageId(mchntImageInfo.getImageId());
                            certBackImage.setAlipayImageId(mchntImageInfo.getAlipayImageId());
                            certBackImage.setWechatImageId(mchntImageInfo.getWechatMediaId());
                            if (!StringUtil.isEmptyOrNull(mchntImageInfo.getImageRemotePath())) {
                                certBackImage.setImagePath(Base64.encodeBase64String(mchntImageInfo.getImageRemotePath().getBytes()));
                            }
                            certBackImage.setImageLocalPath(Base64.encodeBase64String(mchntImageInfo.getImageLocalPath().getBytes()));
                            certBackImage.setImageType(mchntImageInfo.getImageType());
                            certBackImage.setImageDesc(mchntImageInfo.getImageDesc());
                            mchntArtifInfoBean.setCertBackImage(certBackImage);
                        }
                    }
                }

                mchntSignInfoQueryResponse.setBusinessLicenceInfo(businessLicenceInfoBean);
                mchntSignInfoQueryResponse.setMchntArtifInfo(mchntArtifInfoBean);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            } else {
                mchntSignInfoQueryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                mchntSignInfoQueryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                return bizResponse;
            }

        } else {
            //查询正式表
            getMchtSignInfoByFormalTable(mchntSignInfoQueryRequest, mchntSignInfoQueryResponse, bizResponse);
        }


        return bizResponse;
    }

    private void getMchtSignInfoByFormalTable(MchntSignInfoQueryRequest mchntSignInfoQueryRequest, MchntSignInfoQueryResponse mchntSignInfoQueryResponse, BizResponse bizResponse) throws Exception {
        MchntSignInfo mchntSignInfo;/* 查询正式表数据 */
        MchntSignInfoMapper mchntSignInfoMapper = new MchntSignInfoMapperImpl();
        mchntSignInfo = mchntSignInfoMapper.selectMchntSignInfoByPrimaryKey(mchntSignInfoQueryRequest.getHeader().getInstId(), mchntSignInfoQueryRequest.getMchntNo());
        /* 将商户签约信息表中的数据转换成接口响应数据 */
        if (null != mchntSignInfo) {
            BeanUtil.beanCopy(mchntSignInfo, mchntSignInfoQueryResponse);

            BusinessLicenceInfoBean businessLicenceInfoBean = new BusinessLicenceInfoBean();
            BeanUtil.beanCopy(mchntSignInfo, businessLicenceInfoBean);

            MchntArtifInfoBean mchntArtifInfoBean = new MchntArtifInfoBean();
            BeanUtil.beanCopy(mchntSignInfo, mchntArtifInfoBean);
            /* 获取行政区域位置信息 */
            RegionInfoMapper regionInfoMapper = new RegionInfoMapperImpl();
            RegionInfo regionInfo = regionInfoMapper.selectRegionInfoByPrimaryKey(mchntSignInfo.getRegisterProvince());
            if (null != regionInfo) {
                businessLicenceInfoBean.setRegisterProvinceName(regionInfo.getRegionName());
            }
            regionInfo = regionInfoMapper.selectRegionInfoByPrimaryKey(mchntSignInfo.getRegisterCity());
            if (null != regionInfo) {
                businessLicenceInfoBean.setRegisterCityName(regionInfo.getRegionName());
            }
            regionInfo = regionInfoMapper.selectRegionInfoByPrimaryKey(mchntSignInfo.getRegisterArea());
            if (null != regionInfo) {
                businessLicenceInfoBean.setRegisterAreaName(regionInfo.getRegionName());
            }
            /* 获取经营执照影像信息 */
            MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
            MchntImageInfo businessImageInfo = mchntImageInfoMapper.selectMchntImageInfoByPrimaryKey(mchntSignInfoQueryRequest.getHeader().getInstId(),
                    mchntSignInfoQueryRequest.getMchntNo(), ImageTypeEnum.BUSINESS_LICENSE.getImageType(), 0);
            if (null != businessImageInfo) {
                SignImageInfoBean businessImage = new SignImageInfoBean();
                businessImage.setId(businessImageInfo.getId());
                businessImage.setImageIndex(Integer.toString(businessImageInfo.getImageIndex()));
                businessImage.setImageId(businessImageInfo.getImageId());
                businessImage.setAlipayImageId(businessImageInfo.getAlipayImageId());
                businessImage.setWechatImageId(businessImageInfo.getWechatMediaId());
                if (!StringUtil.isEmptyOrNull(businessImageInfo.getImageRemotePath())) {
                    businessImage.setImagePath(Base64.encodeBase64String(businessImageInfo.getImageRemotePath().getBytes()));
                }
                businessImage.setImageLocalPath(Base64.encodeBase64String(businessImageInfo.getImageLocalPath().getBytes()));
                businessImage.setImageType(businessImageInfo.getImageType());
                businessImage.setImageDesc(businessImageInfo.getImageDesc());
                businessLicenceInfoBean.setSignImageInfoBean(businessImage);
            }
            /* 获取法人证件照影像信息 */
            MchntImageInfo faceCertImageInfo = mchntImageInfoMapper.selectMchntImageInfoByPrimaryKey(mchntSignInfoQueryRequest.getHeader().getInstId(),
                    mchntSignInfoQueryRequest.getMchntNo(), ImageTypeEnum.ARTIF_FACE_CERT_IMAGE.getImageType(), 0);
            if (null != faceCertImageInfo) {
                SignImageInfoBean certFaceImage = new SignImageInfoBean();
                certFaceImage.setId(faceCertImageInfo.getId());
                certFaceImage.setImageIndex(Integer.toString(faceCertImageInfo.getImageIndex()));
                certFaceImage.setImageId(faceCertImageInfo.getImageId());
                certFaceImage.setAlipayImageId(faceCertImageInfo.getAlipayImageId());
                certFaceImage.setWechatImageId(faceCertImageInfo.getWechatMediaId());
                if (!StringUtil.isEmptyOrNull(faceCertImageInfo.getImageRemotePath())) {
                    certFaceImage.setImagePath(Base64.encodeBase64String(faceCertImageInfo.getImageRemotePath().getBytes()));
                }
                certFaceImage.setImageLocalPath(Base64.encodeBase64String(faceCertImageInfo.getImageLocalPath().getBytes()));
                certFaceImage.setImageType(faceCertImageInfo.getImageType());
                certFaceImage.setImageDesc(faceCertImageInfo.getImageDesc());
                mchntArtifInfoBean.setCertFaceImage(certFaceImage);
            }
            MchntImageInfo backCertImageInfo = mchntImageInfoMapper.selectMchntImageInfoByPrimaryKey(mchntSignInfoQueryRequest.getHeader().getInstId(),
                    mchntSignInfoQueryRequest.getMchntNo(), ImageTypeEnum.ARTIF_BACK_CERT_IMAGE.getImageType(), 0);
            if (null != backCertImageInfo) {
                SignImageInfoBean certBackImage = new SignImageInfoBean();
                certBackImage.setId(backCertImageInfo.getId());
                certBackImage.setImageIndex(Integer.toString(backCertImageInfo.getImageIndex()));
                certBackImage.setImageId(backCertImageInfo.getImageId());
                certBackImage.setAlipayImageId(backCertImageInfo.getAlipayImageId());
                certBackImage.setWechatImageId(backCertImageInfo.getWechatMediaId());
                if (!StringUtil.isEmptyOrNull(backCertImageInfo.getImageRemotePath())) {
                    certBackImage.setImagePath(Base64.encodeBase64String(backCertImageInfo.getImageRemotePath().getBytes()));
                }
                certBackImage.setImageLocalPath(Base64.encodeBase64String(backCertImageInfo.getImageLocalPath().getBytes()));
                certBackImage.setImageType(backCertImageInfo.getImageType());
                certBackImage.setImageDesc(backCertImageInfo.getImageDesc());
                mchntArtifInfoBean.setCertBackImage(certBackImage);
            }

            mchntSignInfoQueryResponse.setBusinessLicenceInfo(businessLicenceInfoBean);
            mchntSignInfoQueryResponse.setMchntArtifInfo(mchntArtifInfoBean);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            mchntSignInfoQueryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            mchntSignInfoQueryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
        }

    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntSignInfoQueryResponse mchntSignInfoQueryResponse = (MchntSignInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntSignInfoQueryResponse);
    }
}
